package com.ruoyi.basice_info.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.basice_info.domain.TbKnowledgePoints;
import com.ruoyi.basice_info.domain.TsExamDetailsVo;
import com.ruoyi.basice_info.mapper.TbKnowledgePointsMapper;
import com.ruoyi.basice_info.mapper.TsExamDetailsVoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basice_info.mapper.TbUnitsMapper;
import com.ruoyi.basice_info.domain.TbUnits;
import com.ruoyi.basice_info.service.ITbUnitsService;

/**
 * 单元设置Service业务层处理
 *
 * @author hongyi
 * @date 2023-12-25
 */
@Service
public class TbUnitsServiceImpl implements ITbUnitsService
{
    @Autowired
    private TbUnitsMapper tbUnitsMapper;



    // 子表的
    @Autowired
    private TbKnowledgePointsMapper tbKnowledgePointsMapper;

    // 子表的
    @Autowired
    private TsExamDetailsVoMapper tsExamDetailsVoMapper;
    /**
     * 查询单元设置
     *
     * @param unitsId 单元设置主键
     * @return 单元设置
     */
    @Override
    public TbUnits selectTbUnitsByUnitsId(Long unitsId)
    {
        return tbUnitsMapper.selectTbUnitsByUnitsId(unitsId);
    }

    /**
     * 查询单元设置列表
     *
     * @param tbUnits 单元设置
     * @return 单元设置
     */
    @Override
    public List<TbUnits> selectTbUnitsList(TbUnits tbUnits)
    {
        return tbUnitsMapper.selectTbUnitsList(tbUnits);
    }

    /**
     * 新增单元设置
     *
     * @param tbUnits 单元设置
     * @return 结果
     */
    @Override
    public int insertTbUnits(TbUnits tbUnits)
    {
        return tbUnitsMapper.insertTbUnits(tbUnits);
    }

    /**
     * 修改单元设置
     *
     * @param tbUnits 单元设置
     * @return 结果
     */
    @Override
    public int updateTbUnits(TbUnits tbUnits)
    {
        return tbUnitsMapper.updateTbUnits(tbUnits);
    }

    /**
     * 批量删除单元设置
     *
     * @param unitsIds 需要删除的单元设置主键
     * @return 结果
     */
    @Override
    public int deleteTbUnitsByUnitsIds(Long[] unitsIds)
    {
        // 关联两张表
        int num = -1;
        int nums = 0;
        List<Long> longList = Arrays.asList(unitsIds);//将数组转换为list集合
        for (Long item:longList) {
            TsExamDetailsVo detailsVo = new TsExamDetailsVo();
//            detailsVo.setExamDetailsId(item);
            detailsVo.setUnitsId(item);
            List<TsExamDetailsVo> detailsVos = tsExamDetailsVoMapper.selectTsExamDetailsList(detailsVo); // 去子表查询

            TbKnowledgePoints items = new TbKnowledgePoints(); // 子表的对象
            items.setUnitsId(item);
            List<TbKnowledgePoints> list = tbKnowledgePointsMapper.selectTbKnowledgePointsList(items); //去子表查询
            if(list.size()<=0 || detailsVos.size() <=0){
                num=tbUnitsMapper.deleteTbUnitsByUnitsId(item);
            }else{
                nums+=1;
            }
        }
        if(longList.size()==1){
            if(nums>0){
                throw new IllegalArgumentException("该数据在知识点设置中或者考试详情中使用，不能删除");
            }
        }else{
            if(nums>0){
                throw new IllegalArgumentException(nums + " 条数据删除失败，数据在知识点设置中或者考试详情中使用，不能删除");
            }
        }
//        return tbUnitsMapper.deleteTbUnitsByUnitsIds(unitsIds);
        return num;

    }

    /**
     * 删除单元设置信息
     *
     * @param unitsId 单元设置主键
     * @return 结果
     */
    @Override
    public int deleteTbUnitsByUnitsId(Long unitsId)
    {
        return tbUnitsMapper.deleteTbUnitsByUnitsId(unitsId);
    }

    @Override
    public TbUnits getTbUnitsByUnitsCode(String code) {
        return tbUnitsMapper.getTbUnitsByUnitsCode(code);
    }

    @Override
    public TbUnits getTbUnitsByUnitsName(String name) {
        return tbUnitsMapper.getTbUnitsByUnitsName(name);
    }
}
